#include "time.h"
time::time(){}

time::time(short nhour,short nminute):hour(nhour),minute(nminute){}

time::time(float ftime){
	//assuming float is ##.##
	int t = ftime * 100;
	this->minute = t % 100;
	this->hour = t / 100;
}

time::time(double ftime){
	double fractpart, intpart;
	fractpart = modf (ftime , &intpart);
	this->hour = intpart;
	this->minute = ceil((fractpart)*100);
}

time& time::operator=(const time& t){
	if(this != &t){
		this->hour = t.hour;
		this->minute = t.minute;
	}
	return *this;
}

bool time::operator <(const time &t) const{
	if(this->hour < t.hour) 
		return true;
	if(this->hour == t.hour)
		return this->minute < t.minute;
	return false;
}

bool time::operator <=(const time &t) const{
	if(*this < t) 
		return true;
	return (this->hour == t.hour && this->minute == t.minute);
}

time operator-(const time& t1, const time& t2){
	time t;
	if(!(t2<t1))
		throw -1;
	if(t1.minute - t2.minute >=0){
		t.minute = t1.minute - t2.minute;
		t.hour = t1.hour - t2.hour;
	}else{
		t.minute = 60+(t1.minute - t2.minute);
		t.hour = (t1.hour - t2.hour - 1)%24;
	}	
	return t;
}

time operator+(const time& t1, const time& t2){
	time t;	
	if(t1.minute + t2.minute < 60){
		t.minute = t1.minute + t2.minute;
		t.hour = (t1.hour + t2.hour)%24;
	}
	else{
		t.minute = (t1.minute + t2.minute)%60;
		t.hour = (t1.hour + t2.hour + 1)%24;
	}	
	return t;
}

ostream& operator<<(ostream& os, const time& t){
	return os << setw(2) << right << setfill('0') << t.hour << ":" << setw(2) << setfill('0') << t.minute << setfill(' ') << left;
}